import os


class Tag(object):

    def __init__(self, id, name, desc="", parents=None, friends=None):
        self.id = id
        self.name = name
        self.desc = desc
        self.parents = parents
        self.friends = friends

    def set_parent(self, parent):
        if self.parents is None:
            self.parents = []
        self.parents.append(parent)

    def set_friend(self, friend):
        if self.friends is None:
            self.friends = []
        self.friends.append(friend)


class TagMap(object):

    _id_map = {}
    _name_map = {}

    def __init__(self):
        assert False

    @staticmethod
    def find(key):
        if type(key) == int:
            return TagMap._id_map.get(key)
        elif type(key) == str:
            return TagMap._name_map.get(key)
        else:
            assert False, "unknow type"

    @staticmethod
    def add(tag):
        if TagMap._id_map.get(tag.id) is not None:
            return False, 'id already exists'
        if TagMap._name_map.get(tag.id) is None:
            return False, 'name already exists'

        TagMap._id_map[tag.id] = tag
        TagMap._name_map[tag.name] = tag
        return True


ER_SQL = """
    create table tag (
        id int primary key,
        name text unique not null,
        desc text
    );
    
    create parent_ship (
        parent_id int,
        child_id int
    );
    
    create friend_ship (
        tag1 int,
        tag2 int
    );
"""
